import {Component, OnInit, ViewContainerRef} from '@angular/core';
import {Encapsulation} from '../../../../model/Encapsulation';
import {NzModalService} from "ng-zorro-antd/modal";
import {AdminService} from "../../../../services/admin.service";
import {AlertService} from "../../../../services/alert.service";
import {SortingeditComponent} from "../sortingedit/sortingedit.component";

@Component({
  selector: 'app-sortinglist',
  templateUrl: './sortinglist.component.html',
  styleUrls: ['./sortinglist.component.css']
})
export class SortinglistComponent implements OnInit {
  el: Array<Encapsulation>;

  constructor(private modal: NzModalService, private viewContainerRef: ViewContainerRef,
              private adminService: AdminService,
              private alertService: AlertService) {
    this.el = new Array<Encapsulation>();
    this.initData();
  }

  ngOnInit(): void {
    this.initData();
  }


  initData() {
    this.adminService.getEncapsulations().then((data: any) => {
      if (data.msg == "ok") {
        this.el = data.list
      } else {
        this.alertService.showError("获取封装列表失败")
      }
    })
  }

  getEncapsulationByClqk(id: string) {
    this.adminService.getEncapsulationsByClqk(id).then((data: any) => {
      if (data.msg == "ok") {
        this.el = data.list
      } else {
        this.alertService.showError("根据处理情况获取封装列表失败")
      }
    })
  }

  save(id: string) {
    // SortingeditComponent
    const modalwindow = this.modal.create({
      nzTitle: '封装信息编辑',
      nzContent: SortingeditComponent,
      nzViewContainerRef: this.viewContainerRef,
      nzComponentParams: {
        id: id
      },

      nzOnOk: () => new Promise(resolve => {
        let temp = modalwindow.getContentComponent().e;
        if (!temp.proid) {
          this.alertService.showWarning("产品名称必填");
          resolve(false)
        } else if (!temp.cpsl) {
          this.alertService.showWarning("产品数量");
          resolve(false)
        } else {
          modalwindow.getContentComponent().save();
          setTimeout(() => {

            resolve(true);
            this.initData()
            //this.getEmpDetailByEmpid(this.empid);
            //this.loadEmpImg();
          }, 1000)
        }

      })
    })

  }


  generateBcode(id: string) {
    this.adminService.generateBcode(id).then((data: any) => {
      if (data.msg == "ok") {
        // console.dir(data.obj)
        if (data.filepath) {
          this.adminService.downLoadFormFileFor2017XLS(data.filepath)
        }


      } else {
        this.alertService.showError("生产二维码错误")
      }
    })
  }
}
